static HELLO_WORLD: &str = "Hello, world!";

fn main() {
    let x: usize = 5;
    // mut 可以修改
    let mut tup = (0, "hi", 3.14);
    tup.0 = 1;
    println!("The value of x is {x}");
    println!("The value of tup is {:?}", tup);

    let arr = [1; 3];

    println!("The value of arr is {:?}", arr);

    let s1 = String::from("hello");
    println!("The value of s1 is {s1}");

    let a: i32 = 10;
    let b: i64 = 20;
    let c: (i32, &str, i32) = (1, "2", 3);
    let d: [char; 3] = ['1', '2', '3'];

    println!("a = {a}, b = {b}");

    println!("{}", HELLO_WORLD);
    println!("{}", c.1);

    for &i in d.iter() {
        println!("{}", i);
    }
    println!("==============================");

    println!("e = {}", add(1, 2));

    xh();

    let s = String::from("Rust");
    println!("s = {}, len = {}", s, calculate_length(&s));
    println!("s = {}, len = {}", s, calculate_length_str(&s));

    println!("{}", longest("hello", "world"));
}

fn add(a: i32, b: i32) -> i32 {
    a + b
}

fn xh() {
    println!("===================");

    let mut cout = 0;
    let cc = loop {
        cout += 1;
        if cout == 4 {
            // 退出循环
            break cout;
        }
        println!("loop cout = {}", cout);
    };
    println!("===================cc:{cc}");

    while cout < 8 {
        println!("while cout = {}", cout);
        cout += 1;
    }
}

// 借用允许引用数据而不获取所有权 &符号
fn calculate_length(s: &String) -> usize {
    s.len()
}

// &str 支持字符串切片和&String
fn calculate_length_str(s: &str) -> usize {
    s.len()
}

fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
    if x.len() > y.len() { x } else { y }
}
